package com.mongoplus.solon.controller;

import com.mongoplus.mapper.BaseMapper;
import com.mongoplus.solon.entity.User;
import com.mongoplus.solon.service.UserService;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;

import java.time.*;
import java.util.HashMap;

/**
 * 用户控制类
 *
 * @author JiaChaoYang
 **/
@Controller
public class UserController {

    @Inject
    private UserService userService;

    @Inject
    private BaseMapper baseMapper;

    @Get
    @Mapping("/findAllUser")
    public Object findAllUser(){
//        List<Map<String, Object>> mapList = mongoPlusMapMapper.list("user");
        User user = userService.lambdaQuery().orderByDesc(User::getCreateTime).one();
//        Map<String, Object> user = mongoPlusMapMapper.getById("user", null);
        return user;
    }

    @Get
    @Mapping("/addUser")
    public Object addUser(){
        User user = new User();
        user.setUserName("lunzi");
        user.setUserAddress("shenzhen");
        user.setUserAge(18);
        user.setUserStatus(1L);
        user.setCreateTime(LocalDateTime.now());
        user.setTimeout(LocalDateTime.now().plusSeconds(10));
        Boolean save = userService.save(user);

        // Boolean save = baseMapper.save("test_user", new HashMap<String,Object>(){{
        //     put("userName","我要测试我要测试");
        //     put("email", "test");
        //     put("timeout", LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC).plusSeconds(30));
        // }});
        // System.out.println(save?"添加成功":"添加失败");
        return save;
    }

}
